package com.bigdata.demo.methods;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import com.bigdata.demo.entity.Container;
import com.bigdata.demo.entity.image;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;


//远程登录集群
public class Connection_BigData {
    private String hostname ;  //主机IP，如125.221.232.243
    private String username ;  //用户名，如root
    private String password ;  //登录密码，如123@bigdata
    //无参的构造方法
    public Connection_BigData(){}
    public static ArrayList<Container> optimizeSchemeList = new ArrayList<Container>();
    public static ArrayList<image> opList = new ArrayList<image>();
    public static ArrayList<String> Name = new ArrayList<String>();

    //有参的构造方法，主机IP、用户名、登录密码
    public Connection_BigData(String hostname,String username,String password)
    {
        this.hostname = hostname;
        this.username = username;
        this.password = password;
    }
    //连接远程服务器，并运行cmd命令
    public String connection(String cmd)
    {
        String pri; //连接状态，有成功和失败两种
        //指明连接主机的IP地址
        Connection conn = new Connection(hostname);
        Session ssh = null;
        try {
            //连接到主机
            conn.connect();
            //使用用户名和密码校验
            boolean isconn = conn.authenticateWithPassword(username, password);
            if (!isconn)
            {
                System.out.println("用户名称或者是密码不正确");
            }
            else
            {
                System.out.println("已经连接OK");
                ssh = conn.openSession();

                ssh.execCommand(cmd);
                //只允许使用一行命令，即ssh对象只能使用一次execCommand这个方法，
                //多次使用则会出现异常
                //使用多个命令用分号隔开
                //ssh.execCommand("cd /root; sh hello.sh");

                //脚本运行成功，将Terminal屏幕上的输出打印出来
                InputStream is = new StreamGobbler(ssh.getStdout());   //获取运行正常的输出
                BufferedReader brs = new BufferedReader(new InputStreamReader(is));
                Name.clear();
                while (true){
                    String line = brs.readLine();
                    if (line == null){
                        break;
                    }
                    Name.add(line);
                }
                System.out.println(Name);

                //脚本运行失败，将Terminal屏幕上的报错打印出来
                InputStream i = new StreamGobbler(ssh.getStderr());    //获取错误信息
                BufferedReader err = new BufferedReader(new InputStreamReader(i));
                if(err.readLine() != null)       //判断是否有错误信息，不为空则表示脚本运行失败
                {
                    pri = "脚本运行失败\n";
                    pri += "脚本命令为：" + cmd + "\n";
                    pri += "报错如下：";
                    while (true)
                    {
                        String line = err.readLine();
                        if (line == null)
                        {
                            break;
                        }
                        //System.out.println(line);
                        pri += line +"\n";
                    }
                    System.out.println(pri);
                    return pri;
                }
            }
            pri="Created successfully";
        } catch (IOException e)
        {
            e.printStackTrace();
            pri="Created failed";
        } finally
        {
            //连接的Session和Connection对象都需要关闭
            ssh.close();
            conn.close();
        }
        return pri;
    }

    public List<String> connection1(String cmd)
    {
        List<String> ConfDataList = new ArrayList<String>();
        String pri; //连接状态，有成功和失败两种
        //指明连接主机的IP地址
        String info = null;
        Connection conn = new Connection(hostname);
        Session ssh = null;
        try {
            //连接到主机
            conn.connect();
            //使用用户名和密码校验
            boolean isconn = conn.authenticateWithPassword(username, password);
            if (!isconn)
            {
                System.out.println("用户名称或者是密码不正确");
            }
            else
            {
                System.out.println("已经连接OK");
                ssh = conn.openSession();

                ssh.execCommand(cmd);
                //只允许使用一行命令，即ssh对象只能使用一次execCommand这个方法，
                //多次使用则会出现异常
                //使用多个命令用分号隔开
                //ssh.execCommand("cd /root; sh hello.sh");

                //脚本运行成功，将Terminal屏幕上的输出打印出来
                InputStream is = new StreamGobbler(ssh.getStdout());   //获取运行正常的输出
                BufferedReader brs = new BufferedReader(new InputStreamReader(is));
                while (true)
                {
                    String line = brs.readLine();
                    if (line == null)
                    {
                        break;
                    }
                    System.out.println(line);
                    ConfDataList.add(line);
                    info=line;
                }

                //脚本运行失败，将Terminal屏幕上的报错打印出来
                InputStream i = new StreamGobbler(ssh.getStderr());    //获取错误信息
                BufferedReader err = new BufferedReader(new InputStreamReader(i));
                if(err.readLine() != null)       //判断是否有错误信息，不为空则表示脚本运行失败
                {
                    pri = "脚本运行失败\n";
                    pri += "脚本命令为：" + cmd + "\n";
                    pri += "报错如下：";
                    while (true)
                    {
                        String line = err.readLine();
                        if (line == null)
                        {
                            break;
                        }
                        //System.out.println(line);
                        pri += line +"\n";
                    }
                    System.out.println(pri);
                    return ConfDataList ;
//                    return pri;
                }
            }
            pri="Created successfully";
        } catch (IOException e)
        {
            e.printStackTrace();
            pri="Created failed";
        } finally
        {
            //连接的Session和Connection对象都需要关闭
            ssh.close();
            conn.close();
        }
        return  ConfDataList;
//        return info;
    }
}
